package com.showde.shoppingmall.activity;

import java.util.List;

import com.showde.shoppingmall.R;
import com.showde.shoppingmall.adapter.ProductCategoryAdapter;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.ProductCategoryLogic;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.db.ProductCategoryDBCommand;
import com.showde.shoppingmall.domain.ProductCategory;
import com.showde.shoppingmall.utils.ActivityDispatch;

import android.app.ActivityGroup;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class CategoryActivity extends BaseActivity {

	private GridView productCategoryGridView;
	private List<ProductCategory> productCategories;
	private ProductCategoryAdapter productCategoryAdapter;
	private ProductCategoryDBCommand dbCommand;

	private boolean needRefresh = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onResume() {
		super.onResume();
		fetchProductCategoryFromDB();
		refreshData();
	}

	@Override
	public void onClick(View v) {

	}

	@Override
	protected void initParam() {
		@SuppressWarnings("deprecation")
		ActivityGroup activityGroup = ActivityDispatch
				.getActivityGroup(groupTag);
		if (activityGroup == null) {
			groupTag = ActivityDispatch.GROUP_CATEGORY;
		}
		fetchProductCategoryFromNet();
	}

	@Override
	protected void initView() {
		setContentView(R.layout.category_layout);
		productCategoryGridView = (GridView) findViewById(R.id.gl_productcategory);
		productCategoryGridView
				.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						ProductCategory productCategory = (ProductCategory) parent
								.getItemAtPosition(position);
						Bundle bundle = new Bundle();
						bundle.putString(ActivityDispatch.PRODUCT_CATEGORY_ID,
								productCategory.getProductCategoryId());
						ActivityDispatch.openNewActivity(CategoryActivity.this,
								ProductListActivity.class, CategoryGroup.group,
								"CategoryActivity", bundle,
								ActivityDispatch.GROUP_CATEGORY);
					}
				});
	}

	private void refreshData() {
		productCategoryAdapter = (ProductCategoryAdapter) productCategoryGridView
				.getAdapter();
		if (productCategoryAdapter == null) {
			productCategoryAdapter = new ProductCategoryAdapter(this,
					productCategories);
			productCategoryGridView.setAdapter(productCategoryAdapter);
		} else {
			productCategoryAdapter.resetData(productCategories);
		}
		productCategoryAdapter.notifyDataSetChanged();
	}

	private void fetchProductCategoryFromDB() {
		if (dbCommand == null) {
			dbCommand = new ProductCategoryDBCommand(this);
		}
		productCategories = dbCommand.queryAll();
	}

	private void fetchProductCategoryFromNet() {
		// 开启进度条
		ProductCategoryLogic.updateProductCategory(this,
				new UICallbackInterface() {

					@Override
					public void onSuccess(CallbackData data) {
						if (needRefresh) {
							fetchProductCategoryFromDB();
						}
						// 关闭进度条
					}

					@Override
					public void onError(String errorMsg) {
						// 关闭进度条
					}
				});
	}

	private void setNeedRefresh(boolean needRefresh) {
		this.needRefresh = needRefresh;
	}
}
